package com.voute.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.voute.Model.Bean.SearchResultEntity;
import com.voute.R;
import com.voute.Utils.ViewHolderUtils;
import com.voute.View.Activity.FeedDetailInfoActivity;

import java.util.ArrayList;

/**
 * Created by liucxu on 2017/1/14.
 */

public class SearchResultAdapter extends BaseAdapter {
    private ArrayList<SearchResultEntity> datas = new ArrayList<>();
    private Context context;

    public SearchResultAdapter(Context c, ArrayList<SearchResultEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            context = c;
        }
    }

    public void setDatas(ArrayList<SearchResultEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return datas.size();

    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View search_view, ViewGroup viewGroup) {
        if (search_view == null) {
            search_view = View.inflate(context, R.layout.adapter_search_result, null);
        }
        initViews(i, search_view);
        return search_view;
    }

    private void initViews(int position, View search_view) {
        final SearchResultEntity entity = datas.get(position);
        TextView adapter_search_rersult_title = ViewHolderUtils.get(search_view, R.id.adapter_search_rersult_title);
        TextView adapter_search_rersult_time = ViewHolderUtils.get(search_view, R.id.adapter_search_rersult_time);
        TextView tv_left_vote=ViewHolderUtils.get(search_view,R.id.tv_left_vote);
        TextView tv_right_vote=ViewHolderUtils.get(search_view,R.id.tv_right_vote);
        TextView tv_vote_status=ViewHolderUtils.get(search_view,R.id.tv_vote_status);
        adapter_search_rersult_title.setText(entity.getTitle());
        if(TextUtils.isEmpty(entity.getLive_time())){
            adapter_search_rersult_time.setVisibility(View.INVISIBLE);
        }
        else{
            adapter_search_rersult_time.setVisibility(View.VISIBLE);
            adapter_search_rersult_time.setText(entity.getLive_time());
        }
        tv_left_vote.setText(entity.getLeft_option()+"("+entity.getLeft_vote()+")");
        tv_right_vote.setText(entity.getRight_option()+"("+entity.getRight_vote()+")");
        if(entity.isVoted()){
            if(entity.isLeftVote()){
                tv_vote_status.setText("为"+entity.getLeft_option()+"投了"+entity.getSelf_vote()+"票");
            }
            else{
                tv_vote_status.setText("为"+entity.getRight_option()+"投了"+entity.getSelf_vote()+"票");
            }
            tv_vote_status.setVisibility(View.VISIBLE);
        }
        else{
            tv_vote_status.setVisibility(View.GONE);
        }
        search_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, FeedDetailInfoActivity.class);
                intent.putExtra("KEY_FEED_HASH",entity.getHash());
                context.startActivity(intent);
            }
        });
    }
}
